package com.thirtydays.frame.ui.toolbar

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.blankj.utilcode.util.BarUtils
import com.thirtydays.frame.R
import com.thirtydays.frame.databinding.TitleBaseViewBinding


/**
 * @author 小垚
 * @创建时间： 2021/5/22
 * @描述：顶部导航栏
 **/

class TitleToolBar : FrameLayout {

    private var mListener: OnClickListener? = null
    lateinit var viewBinding: TitleBaseViewBinding

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        viewBinding = TitleBaseViewBinding.inflate(LayoutInflater.from(context), this, true)

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleToolBar)

        var isShowBack = typedArray.getBoolean(R.styleable.TitleToolBar_isShowBack, true)

        var backIcon = typedArray.getResourceId(R.styleable.TitleToolBar_backIcon, R.drawable.ic_back_black)

        var centerTitle = typedArray.getString(R.styleable.TitleToolBar_centerTitle)

        var titleTextColor = typedArray.getColor(R.styleable.TitleToolBar_centerTitleColor, Color.BLACK)

        var titleSize = typedArray.getDimension(R.styleable.TitleToolBar_centerTitleSize, 0f)

        var background = typedArray.getColor(R.styleable.TitleToolBar_backgroundColor, Color.WHITE)

        var backgroundResource = typedArray.getResourceId(R.styleable.TitleToolBar_backgroundResource, -1)

        val rightIcon = typedArray.getResourceId(R.styleable.TitleToolBar_rightIcon, -1)

        val rightTitle = typedArray.getString(R.styleable.TitleToolBar_RightTitle)

        val rightColor = typedArray.getColor(R.styleable.TitleToolBar_RightColor, Color.BLACK)

        var isShowLine = typedArray.getBoolean(R.styleable.TitleToolBar_isShowLine, false)

        var isShowBar = typedArray.getBoolean(R.styleable.TitleToolBar_isShowBar, true)


        if (isShowBack) {
            viewBinding.titleBar.run {
                viewBinding.titleBar.setNavigationIcon(backIcon)
                viewBinding.titleBar.setNavigationOnClickListener { v ->
                    if (mListener == null) {
                        if (v.context is Activity) {
                            (v.context as Activity).finish()
                        }
                    } else {
                        mListener?.onClick(v)
                    }

                }
            }
        }

        viewBinding.vTitleLine.visibility = if (isShowLine) View.VISIBLE else View.GONE

        viewBinding.tvCenterTitle.text = centerTitle
        if (titleSize != 0f) {
            viewBinding.tvCenterTitle.textSize = titleSize
        }
        if (titleTextColor != Color.BLACK) {
            viewBinding.tvCenterTitle.setTextColor(titleTextColor)
        }

        if (backgroundResource == -1) {
            rootView.setBackgroundColor(background);
        } else {
            rootView.setBackgroundResource(backgroundResource);
        }

        if (isShowBar) {
            rootView.setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        }
        if (rightIcon != -1) {
            viewBinding.ivRight.setImageResource(rightIcon)
        }

        viewBinding.tvRight.text = rightTitle
        viewBinding.tvRight.setTextColor(rightColor)
    }


    fun setTitle(title: String) {
        viewBinding.tvCenterTitle.text = title
    }

    fun setMenu(menu: Int) {
        viewBinding.titleBar.overflowIcon = resources.getDrawable(R.mipmap.ic_launcher, null)
        viewBinding.titleBar.inflateMenu(menu)
    }

    fun setBack(isShowBack: Boolean, backIcon: Int) {
        if (isShowBack) {
            viewBinding.titleBar.run {
                viewBinding.titleBar.setNavigationIcon(backIcon)
                viewBinding.titleBar.setNavigationOnClickListener { v ->
                    if (mListener == null) {
                        if (v.context is Activity) {
                            (v.context as Activity).finish()
                        }
                    } else {
                        mListener?.onClick(v)
                    }

                }
            }
        }
    }
}